﻿@{
    Layout = "";
}
@using System.Text.Encodings.Web
@model DiscountRules.Standard.Models.RequirementShoppingCartModel
<script>
        $(document).ready(function () {
            $('#saveShoppingCartrequirement@(Model.RequirementId)').click(function () {
                var spentAmount = $("#@Html.IdFor(model => model.SpentAmount)").val();
                var discountId = '@Model.DiscountId';
                var requirementId = '@Model.RequirementId';
                $.ajax({
                    cache:false,
                    type: "POST",
                    url: "@Url.Action("Configure", "ShoppingCartAmount")",
                    data: { "discountId": discountId, "discountRequirementId": requirementId, "spentAmount": spentAmount },
                    success: function (data) {
                         $('#pnl-save-requirement-result@(Model.RequirementId)').fadeIn("slow").delay(1000).fadeOut("slow");
                         //notify parent if it's a new requirement
                         @if (string.IsNullOrEmpty(Model.RequirementId))
                         {
                             <text>$("#discountRequirementContainer").trigger('grandnewdiscountruleadded', [data.NewRequirementId]);</text>
                         }
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert('@Html.Raw(JavaScriptEncoder.Default.Encode(Loc["admin.marketing.Discounts.Requirements.FailedToSave"]))');
                    }
                });
            });
        });
</script>

<div class="form-horizontal">
    <div class="form-group">
        <admin-label asp-for="SpentAmount"/>
        <div class="col-md-9 col-sm-9">
            <div>
                @Html.EditorFor(model => model.SpentAmount)
                <input type="button" id="saveShoppingCartrequirement@(Model.RequirementId)" class="k-button"
                       value="@Loc["Admin.Common.Save"]"/>
            </div>
            <div id="pnl-save-requirement-result@(Model.RequirementId)" style="display:none;">@Loc["admin.marketing.Discounts.Requirements.Saved"]</div>
        </div>
    </div>
</div>